/**
 * Classname : ShowPagingTag.java
 *
 * Version information: 1.0
 *
 * Date: Dec 27, 2010 ho.viet.duan create
 *
 * Planex Viet Name co.ltd, All Rights Reserved.
 */
package com.zho.web.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zho.common.domain.db.PagingInfo;
import com.zho.util.Logit;

/**
 * 
 * @author Ho.Viet.Duan Created Dec 27, 2011
 */
@SuppressWarnings("serial")
public class ShowPagingTag extends TagSupport {  
    private static Logit log = Logit.getInstance(ShowPagingTag.class);

    private PagingInfo pagingInfo;
    private boolean usingURLForNav = true;
    private String navAction;
    private String formId;
    /**
     * @return the pagingInfo
     */
    public PagingInfo getPagingInfo() {
        return pagingInfo;
    }
    /**
     * @param pagingInfo the pagingInfo to set
     */
    public void setPagingInfo(PagingInfo pagingInfo) {
        this.pagingInfo = pagingInfo;
    }
    /**
     * @return the usingURLForNav
     */
    public boolean isUsingURLForNav() {
        return usingURLForNav;
    }
    /**
     * @param usingURLForNav the usingURLForNav to set
     */
    public void setUsingURLForNav(boolean usingURLForNav) {
        this.usingURLForNav = usingURLForNav;
    }
    /**
     * @return the navAction
     */
    public String getNavAction() {
        return navAction;
    }
    /**
     * @param navAction the navAction to set
     */
    public void setNavAction(String navAction) {
        this.navAction = navAction;
    }
    
    private String buildNavAction(String func, int idx) {
        if(usingURLForNav) {
            //https://..../page.shtml?index={idx}
            return (func + idx);
        } else {
            // javascript:navGoto({idx})
            return (func + "(" + idx + ")");
        }
    }
    
    /**
	 * 
	 * @param
	 * @return
	 * @CrDate Jan 18, 2011
	 * @MdDate
	 */
    private String buildNavActionSelect(String name) {
    	String func = "_selectPage"; 
    	if(usingURLForNav) {
            //https://..../page.shtml?index={idx}
            return (func + name);
        } else {
            // javascript:navGoto({idx})
            return (func + "(" + name + ",'" + formId + "')");
        }
    }
    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */
    public int doStartTag() throws JspException {
    	if(pagingInfo == null) {
    		return SKIP_BODY;
    	}
    		
        int pageIndex = pagingInfo.getIndexPage();
        long totalPage = pagingInfo.getTotalPage();
        
        if (totalPage <= 1) return SKIP_BODY;
        
        StringBuffer strNavBuf = new StringBuffer();
        
        String func = "";
        if(usingURLForNav) {
            func = navAction;
        } else {
            func = "javascript:" + navAction;
        }
        
      
        strNavBuf.append("<ul>");
        
        //+++ show previous
        if(pageIndex > 1) {            
        	strNavBuf.append("<li class=\"prew imgimg\"><a href=\"").append(buildNavAction(func, pageIndex - 1)).append("\" >").append("<img src=\"\" /></a></li>");
        	//strNavBuf.append("<button onclick=\"").append(buildNavAction(func, pageIndex - 1)).append("\" ").append(" src=\"/MetaWebBroker/images/icon/prew.gif\"").append(">").append("&lt;").append("</button>");
        } else {
        	strNavBuf.append("<li class=\"prew imgimg\"><a ><img src=\"/images/icon/prew.gif\"  /></a></li>");
        }
       
        
        //strNavBuf.append("<li>");
        //strNavBuf.append("<ul class=\"pages\">");
       
       
        strNavBuf.append("<li class=\"pagingLi\"><select id=\"paging\" name=\"paging\" onchange=\"");
        strNavBuf.append(this.buildNavActionSelect("\'paging\'"));
        strNavBuf.append("\">");
        //+++ show list items
        //List<PagingInfoItem> listPagingItem = pagingInfo.getPagingItems();
      //  List<PagingInfoItem> listPagingItem = pagingInfo.getPagingAllItems();
        for (int j = 1; j <= totalPage; j++) {
        	
        	strNavBuf.append("<option value='").append(j).append("'");
        	if(pageIndex == j) {
        		strNavBuf.append(" selected");
        	}
        	strNavBuf.append(">&nbsp;&nbsp;").append(j).append("&nbsp;/&nbsp;").append(totalPage).append("&nbsp;&nbsp;</option>");
                		
            
        }
        strNavBuf.append("</select></li>");
       
       // strNavBuf.append("</li>");
        
        //+++ show next
        if(totalPage > pageIndex) {
        	strNavBuf.append("<li class=\"prew imgimg\"><a href=\"").append(buildNavAction(func, pageIndex + 1)).append("\" >").append("<img src=\"/MetaWebCustomer/images/icon/next.gif\" /></a></li>");
            //strNavBuf.append("<button onclick=\"").append(buildNavAction(func, pageIndex + 1)).append("\" ").append(" src=\"/MetaWebBroker/images/icon/next.gif\"").append(">").append("&gt;").append("</button>");
        } else {
        	strNavBuf.append("<li class=\"prew imgimg\"><a ><img src=\"\"  /></a></li>");
        }
       
        strNavBuf.append("</ul>");
        
        try {            
            pageContext.getOut().print(strNavBuf.toString()); 
        } catch (Exception e) {
            if (log.ison()) {
                log.debug("doStartTag()", e);
            }
        }
        return SKIP_BODY;
    }
	public String getFormId() {
		return formId;
	}
	public void setFormId(String formId) {
		this.formId = formId;
	}
}
